package com.tuanzi.loan.web.bean.businesssystem;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.businesssystem.BusinessMain;
import com.tuanzi.loan.business.entity.businesssystem.BusinessProduct;
import com.tuanzi.loan.business.service.businesssystem.BusinessMainService;
import com.tuanzi.loan.business.service.businesssystem.BusinessProductService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.businesssystem.BusinessProductRequest;
import com.tuanzi.loan.vo.response.businesssystem.BusinessProductResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.businesssystem.BusinessProductLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class BusinessChannelListBean extends JSFListingBean<BusinessProductRequest, BusinessProductResponse> {
    @Autowired
    BusinessProductService businessProductService;
    @Autowired
    private BusinessMainService settlementCompanyService;

    private List<BusinessMain> companyList;
    @PostConstruct
    public void init() throws Exception {
        companyList = settlementCompanyService.getCompanyList();
        initLazyDataModel();
    }

    private void initLazyDataModel() throws Exception {
        BusinessProductRequest request = (BusinessProductRequest) getParam();
        $ = new BusinessProductRequest();
        if (request!=null)
            $=request;
        lazyDataModel = new BusinessProductLazyDataModel($);
    }

    public void onReset() throws Exception{
        initLazyDataModel();
    }

    public String onAdd(){
        return redirect("/views/businesssystem/product-info.xhtml",$);
    }

    public String onSee(Long id){
        return redirect("/views/businesssystem/product-info.xhtml?id="+id,$);
    }
    public void onDelete(Long id) throws Exception {
        BusinessProduct one = businessProductService.findOne(id);
        businessProductService.delete(one);
        addSuccessfullyMessage();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return  Lists.newArrayList(COOPERATION_MODE, SETTLEMENT_CYCLE,SETTLEMENT_CATEGORY,CHANNEL_TYPE);
    }
}
